﻿
function GetPolicy() {
	var myData = [
			['1', "策略1", "mysql select 预警", "admin", '2012-2-02 12:22:11',   'admin', '2012-2-02 12:22:11' ,'启用'],
			['2', "策略1", "mssql select 预警", "admin", '2012-2-02 12:22:11',   'admin', '2012-2-02 12:22:11' ,'启用'],
			['3', "策略1", "oracle select 预警", "admin", '2012-2-02 12:22:11',   'admin', '2012-2-02 12:22:11' ,'启用'],
			['4', "策略1", "informix select 预警", "admin", '2012-2-02 12:22:11',   'admin', '2012-2-02 12:22:11' ,'启用'],
			['5', "策略1", "db2 select 预警", "admin", '2012-2-02 12:22:11',   'admin', '2012-2-02 12:22:11' ,'启用']

		];                                                                        
	

    // shared reader
    var reader = new Ext.data.ArrayReader({}, [
       			{
							name : 'id'
						}, {
							name : 'policyname'
						}, {
							name : 'desc'
						}, {
							name : 'creator'
						}, {
							name : 'cratetime'
						}, {
							name : 'modifier'
						}, {
							name : 'modifytime'
						}, {
							name : 'status'
						}
    ]);

    var store = new Ext.data.GroupingStore({
            reader: reader,
            data: myData,
            sortInfo:{field: 'id', direction: "ASC"}
        });



	var sm = new Ext.grid.CheckboxSelectionModel();
	var policygrid = new Ext.grid.GridPanel({
				store : store,
				columns : [sm, {
							id : 'id',
							header : 'id',
							width : 10,
							sortable : true,
							dataIndex : 'id',
							hidden : true
						}, {
							header : '策略名',
							width : 70,
							sortable : true,
							dataIndex : 'policyname'
						}, {
							header : '描述',
							width : 100,
							sortable : true,
							dataIndex : 'desc'
						}, {
							header : '创建者',
							width : 70,
							sortable : true,
							dataIndex : 'creator'
						}, {
							header : '创建时间',
							width : 150,
							sortable : true,
							dataIndex : 'cratetime'
						}, {
							header : '修改者',
							width : 70,
							sortable : true,
							dataIndex : 'modifier'
						}, {
							header : '修改时间',
							width : 150,
							sortable : true,
							dataIndex : 'modifytime'
						}, {
							header : '状态',
							width : 70,
							sortable : true,
							dataIndex : 'status'
						}],
				sm : sm,

				tbar : [{
							text : '添加',
							tooltip : '添加',
							iconCls : 'add',
							handler : function() {							
								EditPolicyWindows();
							}
						}, '-', {
							text : '修改',
							tooltip : '修改',
							iconCls : 'modify',
							handler : function() {							
								EditPolicyWindows();
							}
						}, '-', {
							text : '删除',
							tooltip : '删除',
							iconCls : 'delete',
							ref : '../removeBtn',
							disabled : true,
							handler : function() {
								var s = grid.getSelectionModel().getSelections();
								for (var i = 0; r = s[i]; i++) {
									store.remove(r);
								}
							}
						}, '-', {
							text : '刷新',
							tooltip : '刷新',
							iconCls : 'refresh',
							handler : function() {							
								store.loadData(myData);
							}
						}],
				
				stripeRows : true,
				autoExpandColumn : 'id',
				stateful : true,
				stateId : 'policygrid'
			});
	policygrid.getSelectionModel().on('selectionchange', function(sm) {
				policygrid.removeBtn.setDisabled(sm.getCount() < 1);
			});

	function SearchReturn(data){
		var jsondata = Ext.util.JSON.decode(data);
		
	}
	function reflashgriad(page) {
	}
	
	return policygrid;
}

function EditPolicyWindows(){

	var myData = [
					['1', 'mysql'],
					['2', 'mssql'],
					['3', 'oracle'],
					['4', 'informix'],
					['5', 'db2']
				 ];        
	var myData1 = [
					['1', '启用'],
					['2', '禁用']
				 ];        
	var myData2 = [
					['1', '时段1'],
					['2', '时段2'],
					['3', '时段3'],
					['4', '时段4'],
					['5', '时段5']
				 ];        
	var myData3 = [
					['1', '高危'],
					['2', '中危'],
					['3', '低危']
				 ];        

var MyField = new Ext.form.TextField({ 
    name: 'my_field', 
    flex: 1 
}); 

	var AddWindow = new Ext.form.FormPanel({
				frame : true,
				layout : 'form',
				region : 'center',
				xtype : 'fieldset',
				bodyStyle : 'padding:10px;',
				items : [
							 {
									xtype : 'combo',
									name : 'status',
									id : 'status',
									fieldLabel: '状态',
									store : new Ext.data.ArrayStore({
												fields : ['id', 'name'],
												data : myData1
											}),
									valueField : 'id',
									displayField : 'name',
									typeAhead : true,
									mode : 'local',
									triggerAction : 'all',
									editable : false,
									value:'1'
							
		        	},{
								xtype : 'textfield',
								name : '名称',
								fieldLabel : '名称',
								anchor : '100%'
							},{
								xtype : 'textarea',
								name : 'desc',
								fieldLabel : '描述',
								anchor : '100%'
							}, {
									xtype : 'combo',
									name : 'timer',
									id : 'timer',
									fieldLabel: '生效时间',
									store : new Ext.data.ArrayStore({
												fields : ['id', 'name'],
												data : myData2
											}),
									valueField : 'id',
									displayField : 'name',
									typeAhead : true,
									mode : 'local',
									triggerAction : 'all',
									editable : false,
									value:'1'
							
		        	},{
								xtype : 'textfield',
								name : '客户端进程',
								fieldLabel : '客户端进程',
								anchor : '100%',
								listeners : { 
									render : function(field) { 
										new Ext.ToolTip({
							        target : field.el, 
							        anchor: 'top',
							        anchorOffset: 85, 
							        title: '提示',
							        html: '用分号或“|”隔开,例如:sqlyong|navicat'
							    	});
										Ext.QuickTips.init(); 
								 	} 
								}
								
							},{
								xtype : 'checkboxgroup',
								fieldLabel : '客户端IP范围',
								name : 'end',
								id : 'end',
								columns : 2,
								vertical : true,
								items : [{
											xtype : 'textfield',
											name : 'ipstart',
											id   : 'ipstart'
										}, {
											xtype : 'textfield',
											name : 'ipend',
											id   : 'ipend'
										}]
							},{
								xtype : 'textfield',
								name : '例外主机',
								fieldLabel : '例外主机',
								anchor : '100%',
								listeners : { 
									render : function(field) { 
										new Ext.ToolTip({
							        target : field.el, 
							        anchor: 'top',
							        anchorOffset: 85, 
							        title: '提示',
							        html: '用分号或“|”隔开,例如:192.168.0.2|192.168.1.2'
							    	});
										Ext.QuickTips.init(); 
								 	} 
								}
							},new Ext.form.CompositeField({ 
							    fieldLabel: '服务器', 
							    items: [ 
							        MyField, 
							        { 
							            xtype: 'button', 
							            text: '选择' ,
							            handler: function() { 
							            		
							                selectServer();
							            } 
							        } 
							    ] 
							}) ,{
										xtype: 'checkboxgroup',
										itemCls: 'x-check-group-alt',
										fieldLabel: '数据库',
										columns: 4,
										id:'db',
										name:'db',
										items: [
										{boxLabel: 'mysql', name: 'mysql',id:'mysql'},
										{boxLabel: 'mssql', name: 'mssql',id:'mssql'},
										{boxLabel: 'oracle', name: 'oracle',id:'oracle'},
										{boxLabel: 'informix', name: 'informix',id:'informix'},
										{boxLabel: 'db2', name: 'db2',id:'db2'}
										
										]
							},{
								xtype : 'textfield',
								name : '表格',
								fieldLabel : '表格',
								anchor : '100%',
								listeners : { 
									render : function(field) { 
										new Ext.ToolTip({
							        target : field.el, 
							        anchor: 'top',
							        anchorOffset: 85, 
							        title: '提示',
							        html: '用分号或“|”隔开,例如:table1|table2'
							    	});
										Ext.QuickTips.init(); 
								 	} 
								}
							        
							},{
										xtype: 'checkboxgroup',
										itemCls: 'x-check-group-alt',
										fieldLabel: '操作',
										columns: 4,
										id:'operate',
										name:'operate',
										items: [
										{boxLabel: 'select(选择)', name: 'select',id:'select'},
										{boxLabel: 'insert(插入)', name: 'insert',id:'insert'},
										{boxLabel: 'update(更新)', name: 'update',id:'update'},
										{boxLabel: 'delete(删除)', name: 'delete',id:'delete'},
										{boxLabel: 'truncate(删表)', name: 'truncate',id:'truncate'},
										{boxLabel: 'create(创建)', name: 'create',id:'create'},
										{boxLabel: 'grant(允许)', name: 'grant',id:'grant'},
										{boxLabel: 'drop(删对象)', name: 'drop',id:'drop'},
										{boxLabel: 'rollback(回滚)', name: 'rollback',id:'rollback'},
										{boxLabel: 'alter(改变)', name: 'alter',id:'alter'},
										{boxLabel: 'logout(登出)', name: 'logout',id:'logout'},
										{boxLabel: 'login(登录)', name: 'login',id:'login'},
										{boxLabel: 'call(过程)', name: 'call',id:'call'},
										{boxLabel: 'execute(执行)', name: 'execute',id:'execute'}
										]
							},{
									xtype : 'combo',
									name : 'level',
									id : 'level',
									fieldLabel: '级别',
									store : new Ext.data.ArrayStore({
												fields : ['id', 'name'],
												data : myData3
											}),
									valueField : 'id',
									displayField : 'name',
									typeAhead : true,
									mode : 'local',
									triggerAction : 'all',
									editable : false,
									value:'2'
							},{
										xtype: 'checkboxgroup',
										itemCls: 'x-check-group-alt',
										fieldLabel: '通知',
										columns: 4,
										id:'inform',
										name:'inform',
										items: [
										{boxLabel: '通知管理员', name: 'mamger',id:'mamger'},
										{boxLabel: '通知安全员', name: 'safetyman',id:'safetyman'},
										{boxLabel: '通知审计员', name: 'auditor',id:'auditor'}
										
										]
							}
						]

			});

	var addWindows = new Ext.Window({
				layout : 'border',
				width : 700,
				height : 600,
				title : "添加",
				modal : true,
				resizable : true,
				maximizable : true,
				iconCls : 'search',
				items : [AddWindow],
				plain : true
			});
	addWindows.addButton({
				text : "确 定",
				handler : function() {
					
					addWindows.close();
				}
			});
	addWindows.addButton({
				text : '取 消',
				handler : function() {
					addWindows.close();
				}
			});
	addWindows.show();
}

function selectServer(){
	var myData = [
			['1', "DB1", "mysql", "192.168.0.2", '3306',   'testpc1'],
			['2', "DB2", "mssql", "192.168.0.3", '1433',   'testpc2'],
			['3', "DB3", "orcale", "192.168.0.4", '7088',   'testpc3'],
			['4', "DB4", "informix", "192.168.0.5", '9088',   'testpc4'],
			['5', "DB4", "db2", "192.168.0.6", '5855',   'testpc5']
		];                                                                        
	

    // shared reader
    var reader = new Ext.data.ArrayReader({}, [
       			{
							name : 'id'
						}, {
							name : 'server'
						}, {
							name : 'db'
						}, {
							name : 'ip'
						}, {
							name : 'port'
						}, {
							name : 'desc'
						}
    ]);

    var store = new Ext.data.GroupingStore({
            reader: reader,
            data: myData,
            sortInfo:{field: 'id', direction: "ASC"}
        });



	var sm = new Ext.grid.CheckboxSelectionModel();
	var grid = new Ext.grid.GridPanel({
				store : store,
				columns : [sm, {
							id : 'id',
							header : 'id',
							width : 10,
							sortable : true,
							dataIndex : 'id',
							hidden : true
						}, {
							header : '服务器',
							width : 70,
							sortable : true,
							dataIndex : 'server'
						}, {
							header : '数据库',
							width : 100,
							sortable : true,
							dataIndex : 'db'
						}, {
							header : '服务器IP',
							width : 70,
							sortable : true,
							dataIndex : 'ip'
						}, {
							header : '服务器端口',
							width : 70,
							sortable : true,
							dataIndex : 'port'
						}, {
							header : '描述',
							width : 100,
							sortable : true,
							dataIndex : 'desc'
						}],
				sm : sm,

				
				
				stripeRows : true,
				autoExpandColumn : 'id',
				stateful : true,
				region : 'center',
				frame : true,
				stateId : 'grid'
			});
	
	function SearchReturn(data){
		var jsondata = Ext.util.JSON.decode(data);
		
	}
	function reflashgriad(page) {
	}

	
		var addWindows = new Ext.Window({
				layout : 'border',
				width : 500,
				height : 300,
				title : "添加",
				modal : true,
				resizable : true,
				maximizable : true,
				iconCls : 'search',
				items : [grid],
				plain : true
			});
	addWindows.addButton({
				text : "确 定",
				handler : function() {
					
					addWindows.close();
				}
			});
	addWindows.addButton({
				text : '取 消',
				handler : function() {
					addWindows.close();
				}
			});
	addWindows.show();
}